package com.llkj.qingnianguoxue.base;

import android.content.Context;

import com.activeandroid.ActiveAndroid;
import com.jm.chat.application.JGApplication;
import com.kk.taurus.playerbase.config.PlayerConfig;
import com.kk.taurus.playerbase.config.PlayerLibrary;
import com.llkj.qingnianguoxue.util.SpUtil;
import com.mob.MobSDK;
import com.zhy.http.okhttp.OkHttpUtils;
import org.litepal.LitePalApplication;
import java.util.concurrent.TimeUnit;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.content.EventNotificationContent;
import cn.jpush.im.android.api.enums.ContentType;
import cn.jpush.im.android.api.event.MessageEvent;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.Message;

/**
 *
 */
public class MyApplication extends JGApplication {

    public SpUtil sputil;
    public static boolean ignoreMobile;

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        sputil = SpUtil.init(this,
                ConstantUserInfo.SP_USERINFO_FILE_NAME, Context.MODE_PRIVATE);

        okhttp3.OkHttpClient okHttpClient = OkHttpUtils.getInstance().getOkHttpClient().newBuilder()
                .connectTimeout(20000, TimeUnit.MILLISECONDS)
                .readTimeout(20000, TimeUnit.MILLISECONDS).build();
        OkHttpUtils.initClient(okHttpClient);

        // 初始化ShareSDK
        MobSDK.init(this);
        LitePalApplication.initialize(this);

        PlayerConfig.setUseDefaultNetworkEventProducer(true);
        //初始化播放器
        PlayerLibrary.init(this);

        //注册sdk的event用于接收各种event事件
        JMessageClient.registerEventReceiver(this);
    }


    @Override
    public void onTerminate() {
        super.onTerminate();
        //事件接收类的解绑
        JMessageClient.unRegisterEventReceiver(this);
    }

    public void onEvent(MessageEvent event) {
        try {
            final Message message = event.getMessage();
            //若为群聊相关事件，如添加、删除群成员
            if (message.getContentType() == ContentType.eventNotification) {
                GroupInfo groupInfo = (GroupInfo) message.getTargetInfo();
                long groupId = groupInfo.getGroupID();
                EventNotificationContent.EventNotificationType type = ((EventNotificationContent) message
                        .getContent()).getEventNotificationType();
                switch (type) {
                    case group_dissolved:
                        JMessageClient.deleteGroupConversation(groupId);
                        break;
                }
            }
        }catch (Exception e){}
    }

    /**
     * 单例模式
     */
    private static MyApplication application;

    public static MyApplication getInstance() {
        return application;
    }

}
